package com.system.healthproject.model.sickuser.controller;

import com.system.healthproject.bean.ParamterUpdateDoctorInfo;
import com.system.healthproject.bean.web.ResultCard;
import com.system.healthproject.bean.web.ResultOrder;
import com.system.healthproject.bean.web.ResultPatientAccount;
import com.system.healthproject.bean.web.ResultSickuserAccountDetailed;
import com.system.healthproject.common.ApiResult;
import com.system.healthproject.model.sickuser.service.AccountInfoService;
import com.system.healthproject.utils.AlipayConfig;
import com.system.healthproject.utils.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 患者账户信息
 */
@Api(value = "患者端 账户信息" ,tags = "患者账户信息数据接口api")
@RequestMapping("/sickuser/account")
@Controller
public class AccountInfoController {
    @Autowired
    private RedisUtil redisUtil;
    @Autowired(required = false)
    private AccountInfoService accountInfoService;

    @RequestMapping(value = "/selectSickuserAccount" ,method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ApiOperation(value = "查询患者账户信息(接口已调用)",notes = "查询患者账户信息",httpMethod = "post",response = ApiResult.class)
    @ResponseBody
    public ApiResult selectSickuserAccount(@RequestHeader("token") String token){
        Map<String, Object> map = (Map<String, Object>) redisUtil.get(token);
        Integer loginId = (Integer) map.get("loginId");
        ResultPatientAccount patientAccount = accountInfoService.selectSickuserAccount(loginId);
        return ApiResult.successWithData("成功",patientAccount);
    }

    @RequestMapping(value = "/selectSickuserAccountDetailed" ,method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ApiOperation(value = "账户信息，查看明细(接口已调用)",notes = "账户明细",httpMethod = "post",response = ApiResult.class)
    @ResponseBody
    public ApiResult selectSickuserAccountDetailed(@RequestHeader("token") String token){
        Map<String, Object> map = (Map<String, Object>) redisUtil.get(token);
        Integer loginId = (Integer) map.get("loginId");
        List<ResultSickuserAccountDetailed> sickuserAccountDetailed = accountInfoService.selectSickuserAccountDetailed(loginId);
        return ApiResult.successWithData("成功",sickuserAccountDetailed);
    }

    @RequestMapping(value = "/selectCard" ,method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ApiOperation(value = "账户信息  充值卡(接口已调用)",notes = "账户明细",httpMethod = "post",response = ApiResult.class)
    @ResponseBody
    public ApiResult selectCard(){
        List<ResultCard> list = accountInfoService.selectCard();
        return ApiResult.successWithData("成功",list);
    }

    @RequestMapping(value = "/goRecharge" ,method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ApiOperation(value = "账户信息，去充值(接口已调用)",notes = "充值（产生订单）",httpMethod = "post",response = ApiResult.class)
    @ResponseBody
    @ApiImplicitParam(name = "cardId",value = "充值卡ID",dataType = "int")
    public ApiResult goRecharge(@RequestHeader("token") String token,Integer cardId){
        Map<String, Object> map = (Map<String, Object>) redisUtil.get(token);
        Integer loginId = (Integer) map.get("loginId");
        ResultOrder order = accountInfoService.goRecharge(loginId,cardId);
        return ApiResult.successWithData("成功",order);
    }


    @RequestMapping(value = "/recharge" ,method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ApiOperation(value = "充值（支付）",notes = "患者充值",httpMethod = "post",response = ApiResult.class)
    @ResponseBody
    @ApiImplicitParam(name = "cardorderId",value = "订单ID",dataType = "int")
    public ApiResult recharge(@RequestHeader("token") String token,Integer cardorderId) {
        Map<String, Object> map = (Map<String, Object>) redisUtil.get(token);
        Integer loginId = (Integer) map.get("loginId");



        /*//获得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type);
        //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(AlipayConfig.return_url);
        alipayRequest.setNotifyUrl(AlipayConfig.notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = request.getParameter("WIDout_trade_no");
        //付款金额，必填
        String total_amount = request.getParameter("WIDtotal_amount");
        //订单名称，必填
        String subject =request.getParameter("WIDsubject");
        //商品描述，可空
        String body = request.getParameter("WIDbody");

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        //请求
        String result;
        try {
            result = alipayClient.pageExecute(alipayRequest).getBody();
            response.setContentType("text/html;charset=" + AlipayConfig.charset);
            response.getWriter().write(result);//直接将完整的表单html输出到页面
            response.getWriter().flush();
            response.getWriter().close();
        } catch (AlipayApiException e) {
            e.printStackTrace();
            try {
                response.getWriter().write("捕获异常出错");
                response.getWriter().flush();
                response.getWriter().close();
            response.getWriter().write("捕获异常出错");
            response.getWriter().flush();
            response.getWriter().close();
        }
*/

       /*     } catch (IOException ioException) {
                ioException.printStackTrace();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }


        return ApiResult.successWithData("成功");
        */
       return null;
    }
}
